package com.myPlaneGame.obj;

import com.myPlaneGame.GameCreate;
import com.myPlaneGame.Utils.GameUtils;

import java.awt.*;

public class LittleBoss2Bullet extends GameObj{
    int health=3;
    int timeCount=50;//控制跟踪速度(每个子弹的计数器都是独立的50)
    public LittleBoss2Bullet() {
        super();
    }

    public LittleBoss2Bullet(Image img, int width, int height, int x, int y, double speed, GameCreate frame) {
        super(img, width, height, x, y, speed, frame);
    }

    public LittleBoss2Bullet(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public LittleBoss2Bullet(int x, int y) {
        super(x, y);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //添加子弹追踪功能
        timeCount--;
        if(timeCount>0) {
            //通过判断boss2子弹和我方飞机的位置左右来改变子弹的x值
            this.y = this.y - ((this.y - GameUtils.gameObjList.get(GameCreate.planeIndex).getY()) / 30);
            this.x = this.x - ((this.x - GameUtils.gameObjList.get(GameCreate.planeIndex).getX()) / 10);//10是缩小子弹的差值（控制追踪导弹的横向速度）
        }
        else {
            this.y+= speed;
        }
        //1.敌方boss2的子弹和我方子一级子弹碰撞后，我方子弹
        for (ShellObj shellObj : GameUtils.shellObjList) {
            if (this.getRec().intersects(shellObj.getRec()) && health > 0) {
                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                health--;
            } else if (this.getRec().intersects(shellObj.getRec())) {
                //boss2生命值为0之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                shellObj.setX(-100);
                shellObj.setY(-100);
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(shellObj);

            }
        }
        //2.敌方boss2的子弹和我方二级子弹碰撞后，我方子弹消失
        for (DoubleShellObj doubleShellObj : GameUtils.doubleShellObjList) {
            if (this.getRec().intersects(doubleShellObj.getRec()) && health > 0) {
                doubleShellObj.setX(-100);
                doubleShellObj.setY(-100);
                GameUtils.removeList.add(doubleShellObj);
                health-=2;
            } else if (this.getRec().intersects(doubleShellObj.getRec())) {
                //boss2生命值为0之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                doubleShellObj.setX(-100);
                doubleShellObj.setY(-100);
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(doubleShellObj);
            }
        }
        //3.敌方boss2的子弹和我方三级子弹碰撞后，我方子弹消失和敌方子弹消失（击毁追踪导弹）
        for (TripleShellObj tripleShellObj : GameUtils.tripleShellObjList) {
            if (this.getRec().intersects(tripleShellObj.getRec()) && health > 0) {
                tripleShellObj.setX(-100);
                tripleShellObj.setY(-100);
                GameUtils.removeList.add(tripleShellObj);
                health-=3;
            } else if (this.getRec().intersects(tripleShellObj.getRec())) {
                //boss2生命值为0之后出现爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                tripleShellObj.setX(-100);
                tripleShellObj.setY(-100);
                this.x = -200;
                this.y = -200;
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(tripleShellObj);
            }
        }
        //技能开启保护网
        if(PlaneObj.skillUsing==2){
            if (this.y>450&&this.y<800) {
                this.x=-1000;
                this.y=-1000;
                GameUtils.removeList.add(this);
            }
        }
        //技能清除子弹
        if(PlaneObj.skillUsing==1){
            this.x=-1000;
            this.y=-1000;
        }
        //越界判断：清除跃出游戏窗口的数据（降低游戏页面绘制的压力）
        if(this.y>1000||this.y<-100){
            this.x=-1000;
            this.y=-1000;
            GameUtils.removeList.add(this);
        }
        //重新刷新游戏数据
        if(GameCreate.state==3||GameCreate.state==4){
            this.x=-1000;
            this.y=-1000;
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}

